//
//  HSYWithDrawCodeViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/7.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYWithDrawCodeViewController.h"
#import "HSYWithDrawCodeRequest.h"
#import "HSYWithDrawRequest.h"
#import "BQActivityView.h"

@interface HSYWithDrawCodeViewController ()

@property(nonatomic,assign)NSInteger second;
@property(nonatomic,strong)NSTimer *timer;

@property (weak, nonatomic) IBOutlet UIButton *getIdentifyingCodeBtn;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;

@property (weak, nonatomic) IBOutlet UITextField *codeTextFiled;

@property (nonatomic, strong) HSYWithDrawCodeRequest *codeTask;

@end

@implementation HSYWithDrawCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tipLabel.text = [NSString stringWithFormat:@"短信已发送至  %@****%@",[[HSYUserCenter instatce].appAuUser.cellphone substringToIndex:3],[[HSYUserCenter instatce].appAuUser.cellphone substringFromIndex:6]];
    
    [self getIdentifyingCodeBtnClcik:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)closedBtnClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)getIdentifyingCodeBtnClcik:(id)sender {
    self.getIdentifyingCodeBtn.enabled = NO;
    //开始倒计时
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateGetIdentifyingCodeBtnText) userInfo:nil repeats:YES];

    if (self.codeTask) {
        [self.codeTask cancelCurrentTask];
    }
    
    //发送验证码
    HSYWithDrawCodeRequest *task = [[HSYWithDrawCodeRequest alloc] init];
    task.accountId = [HSYUserCenter instatce].appAuUser.accountID;
    self.codeTask = task;
    
    [HSYNetworkManager POST:task success:nil failure:nil];
}

-(void)updateGetIdentifyingCodeBtnText{
    NSString *time = [NSString stringWithFormat:@"%lds",(long)(60- self.second)];
    [self.getIdentifyingCodeBtn setTitle:time forState:UIControlStateNormal];
    
    if (self.second == 60) {
        [self.timer invalidate];
        self.getIdentifyingCodeBtn.enabled = YES;
        self.second = 0;
        [self.getIdentifyingCodeBtn setTitle:@"重新获得短信验证码" forState:UIControlStateNormal];
        
        return;
    }
    
    self.second++;
    
    
}


- (IBAction)makeSureBtnClick:(UIButton *)sender {
    
    if (!self.codeTextFiled.text.length) {
        [self showMessage:@"请输入验证码"];
    }
    
    //点击确认提交，发送提现请求
    HSYWithDrawRequest *task = [[HSYWithDrawRequest alloc] init];
    task.accountId = [HSYUserCenter instatce].appAuUser.accountID;
    task.totalAmount = self.money;
    task.channel = @"101";
    task.verifyCode = self.codeTextFiled.text;
    
    [BQActivityView showActiviTy];
    __weak typeof(self)weakSelf = self;

    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        [BQActivityView hideActiviTy];
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            NSDictionary *dict = response.encryptDataResult;
            
            if ([dict[@"code"] intValue] == -1) {
                [weakSelf showMessage:dict[@"msg"]];
            }else{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1ull * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    //1秒后回调方法
                    [strongSelf passedValidation];
                });
                [weakSelf closedBtnClick:nil];
            }
        }else{
            [weakSelf showMessage:response.resultMessage];
        }
    } failure:^(NSError *error) {
        [BQActivityView hideActiviTy];

        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];
    
}

//验证通过
- (void)passedValidation{
    if (self.delegate && [self.delegate respondsToSelector:@selector(hsyWithDrawCodeViewControllerDidPassedValidation:)]) {
        [self.delegate hsyWithDrawCodeViewControllerDidPassedValidation:self];
    }

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
